/*
 * Copyright (c) 2011, Andrew Cole
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.andycole.xmiwriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

import net.andycole.netbeans.uml.UmlPackage;
import org.netbeans.api.xmi.XMIWriter;
import org.netbeans.lib.jmi.xmi.OutputConfig;
//import org.netbeans.api.xmi.XMIWriterFactory;

import net.andycole.xmiparser.model.IModel;

/**
 * Writes the contents of a NBModelImpl to an xmi file.
 * @author Andrew Cole
 */
public class NetbeansModelWriter implements IXMIWriter
{

   private File outputFile;
   private IModel model;
   private String encoding;

   private Logger LOG = Logger.getLogger(NetbeansModelWriter.class);

   public NetbeansModelWriter(File     outputFile,
                              IModel   model)
   {
      this.outputFile = outputFile;
      this.model = model;
      this.encoding = "UTF-8";
   }

   /**
    * Write Netbeans UML-specific model content
    */
   @Override
   public void write()
   {
      FileOutputStream stream = null;
      try
      {
         UmlPackage topPackage = (UmlPackage)model.getBasePackage();
         OutputConfig config = new OutputConfig();
         config.setEncoding(encoding);
         XMIWriter writer = new NetbeansXmiWriter(config);
         //XMIWriter writer = XMIWriterFactory.getDefault().createXMIWriter(config);

         stream = new FileOutputStream(outputFile);
         writer.write(stream, topPackage, "1.4");
      }
      catch (Exception error)
      {
//         System.err.println("Could not write to output File "+
//                            outputFile +": " +error.getMessage());
         LOG.error("Could not write to output File "+
                   outputFile + ": "+error.getMessage());
      }
      finally
      {
         try{
            if (stream != null)
               stream.close();
         }
         catch(IOException e){
//            System.err.println("Could not close file stream "+e);
            LOG.warn("Could not close file stream "+e);
         }
      }
   }

}
